﻿using AutoMapper;
using MediatR;
using S.PurchaseAndSales.Api.Application.Command.PurchaseFormCommand;
using S.PurchaseAndSales.Domain.Model;
using S.PurchaseAndSales.Infrastructure.Interface.PurchaseForm;
using Yitter.IdGenerator;

namespace S.PurchaseAndSales.Api.Application.CommandHandlers.PurchaseFormHandler
{
    /// <summary>
    /// 供应商添加
    /// </summary>
    public class VendorAddCommandHandler : IRequestHandler<VendorAddCommand, bool>
    {
        private readonly IVendorRepository repository;
        private readonly IMapper mapper;

        public VendorAddCommandHandler(IVendorRepository repository,IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<bool> Handle(VendorAddCommand request, CancellationToken cancellationToken)
        {
            YitIdHelper.SetIdGenerator(new IdGeneratorOptions(0));
            request.CreateBy = "郭佳";
            request.VenDorCode = YitIdHelper.NextId();
            return await repository.AddAsync(mapper.Map<VendorModel>(request));
        }
    }
}
